package net.mimai.vultrue.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.mimai.vultrue.bus.enums.SimpleStatus;
import net.mimai.vultrue.domain.BcmOperatorDomain;
import net.mimai.vultrue.framework.base.enums.SystemCode;
import net.mimai.vultrue.framework.common.token.IOperatorLoginService;
import net.mimai.vultrue.framework.common.token.domain.LoginRequest;
import net.mimai.vultrue.framework.common.token.domain.LoginResponse;
import net.mimai.vultrue.framework.context.token.SessionUser;
import net.mimai.vultrue.framework.utils.FrameworkUtils;
import net.mimai.vultrue.framework.utils.JudgeUtils;
import net.mimai.vultrue.service.trans.IXBcmOperatorTransService;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

/**
 * @ClassName OperatorLoginService
 * @Description TODO
 * @Author 方丈
 * @Date 2021/11/5 17:08
 * @Version 1.0.0
 **/
@Component
public class OperatorLoginService implements IOperatorLoginService {

    @Resource
    IXBcmOperatorTransService operatorTransService;

    @Override
    public LoginResponse login(LoginRequest loginRequest) {

        String userName = loginRequest.getUserName();
        QueryWrapper<BcmOperatorDomain> operatorDomainQueryWrapper = new QueryWrapper<>();
        operatorDomainQueryWrapper.lambda()
                .eq(BcmOperatorDomain::getMblNo,userName)
                .eq(BcmOperatorDomain::getStatus, SimpleStatus.NORMAL.getKey());
        BcmOperatorDomain operatorDomain = operatorTransService.getOne(operatorDomainQueryWrapper);

        String passwd = loginRequest.getPasswd();
        String pwd = passwd + operatorDomain.getEncSalt();
        String md5Str= DigestUtils.md5DigestAsHex(pwd.getBytes());

        if(!JudgeUtils.equalsIgnoreCase(md5Str,operatorDomain.getEncPwd())){
            FrameworkUtils.throwFailRet(SystemCode.LOGIN_PWD_ERROR);
        }
        LoginResponse loginResponse =new LoginResponse();
        SessionUser sessionUser =new SessionUser();
        sessionUser.setUserNo(operatorDomain.getOprNo());
        sessionUser.setUserNm(operatorDomain.getOprName());
        sessionUser.setUserCredentials(operatorDomain.getMblNo());
        loginResponse.setTokenInfo(sessionUser);
        return loginResponse;
    }

}
